﻿# coding: utf-8

from Command import CCommand

import Transformation
import SceneNode

class CCmdRemoveModel(CCommand):
    def __init__(self, **p):
        """
        Default class contructor.
        
        :param 'modelNodeRef': weak reference of the node which need to be remove.
        """
                
        CCommand.__init__(self, TD.CMD_REMOVE_MODEL, **p)
        
        self._fileName = ''
        
    def DoCmd(self):
        try:   
            nodeRef = self._paramDict['modelNodeRef']
            node = nodeRef()
            if not node:
                return
                
            coreHandle = node.GetCoreHandle()
            
            self._tx, self._ty, self._tz = Transformation.GetRelativeTranslation(SceneNode._CTransformation(coreHandle))
            self._rx, self._ry, self._rz, self._rw = Transformation.GetRelativeRotation(SceneNode._CTransformation(coreHandle))
            self._sx, self._sy, self._sz = Transformation.GetRelativeScale(SceneNode._CTransformation(coreHandle))
                            
            SCENE_EDITOR.RemoveSceneNode(node)
            self._fileName = node._fileName
            
        except:
            SHOW_TRACE()
                    
    def UndoCmd(self):
        try:
            modelNodeRef, entityHandle = SCENE_EDITOR.CreateModelSceneNode(self._fileName, True)
            self._paramDict['modelNodeRef'] = modelNodeRef
            coreHandle = modelNodeRef().GetCoreHandle()
            Transformation.SetRelativeTranslation(SceneNode._CTransformation(coreHandle), self._tx, self._ty, self._tz)
            Transformation.SetRelativeRotation(SceneNode._CTransformation(coreHandle), self._rx, self._ry, self._rz, self._rw)
            Transformation.SetRelativeScale(SceneNode._CTransformation(coreHandle), self._sx, self._sy, self._sz)
        
        except:
            SHOW_TRACE()
        
        return True